package com.zzyl.member.controller;

import com.github.pagehelper.Page;
import com.zzyl.common.core.controller.BaseController;
import com.zzyl.common.core.domain.AjaxResult;
import com.zzyl.common.core.domain.R;
import com.zzyl.common.utils.PageUtils;
import com.zzyl.member.domain.Reservation;
import com.zzyl.member.dto.ReservationCountDTO;
import com.zzyl.member.dto.ReservationDto;
import com.zzyl.member.service.IReservationService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.Instant;
import java.time.LocalDate;
import java.time.ZoneId;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 预约信息Controller
 *
 * @author ruoyi
 */
@RestController
@RequestMapping("/member/reservation")
@Api(tags = "预约信息相关接口")
public class MemberReservationController extends BaseController {
    @Autowired
    private IReservationService reservationService;

    /**
     * 查询取消预约数量
     * @return
     */
    @GetMapping("/cancelled-count")
    @ApiOperation(value = "查询取消预约数量", notes = "获取已被取消的预约记录总数")
    public Map<String, Object> getCancelledReservationCount() {
        Integer cancelledCount = reservationService.getCancelledCount();
        Map<String, Object> result = new HashMap<>();
        result.put("code", 200);
        result.put("msg", "操作成功");
        result.put("data", cancelledCount);
        return result;
    }

    /**
     * 分页查询预约信息列表
     */
    @GetMapping("/page")
    @ApiOperation(value = "分页查询预约信息列表", notes = "根据条件分页获取预约信息列表")
    public Map<String, Object> list(@ApiParam(value = "预约信息查询条件") Reservation reservation) {
        // 开启分页
        PageUtils.startPage();

        // 查询数据
        List<Reservation> list = reservationService.selectReservationList(reservation);

        // 强转为 Page，获取 total
        Page<Reservation> page = (Page<Reservation>) list;

        // 手动封装成你要的结构
        Map<String, Object> data = new HashMap<>();
        data.put("total", page.getTotal());
        data.put("rows", page.getResult());
        data.put("code", 200);
        data.put("msg", "请求成功");

        Map<String, Object> result = new HashMap<>();
        result.put("code", 200);
        result.put("msg", "操作成功");
        result.put("data", data);

        return result;
    }

    /**
     * 统计可以预约数量
     * @param timestamp
     * @return
     */
    @GetMapping("/countByTime")
    @ApiOperation(value = "按时间统计预约数量", notes = "根据指定日期统计预约数量")
    public R<?> countByTime(@ApiParam(value = "时间戳（毫秒）", example = "1618934400000") @RequestParam("time") Long timestamp) {
        LocalDate date = Instant.ofEpochMilli(timestamp)
                .atZone(ZoneId.systemDefault())
                .toLocalDate();

        List<ReservationCountDTO> result = reservationService.countByTime(date);
        return R.ok(result);
    }
    /**
     * 新增预约
     */
    //@PreAuthorize("@ss.hasPermi('member:reservation:insertReservation')")
    @PostMapping
    @ApiOperation(value = "新增预约", notes = "创建一个新的预约记录")
    public AjaxResult add(@ApiParam(value = "预约信息") @RequestBody ReservationDto reservationDto) {
        reservationService.insertReservation(reservationDto);
        return success();
    }
    /**
     * 修改预约
     */
    @PutMapping("/{id}/cancel")
    @ApiOperation(value = "取消预约", notes = "根据预约ID取消指定预约")
    public AjaxResult updateById(@ApiParam(value = "预约ID", example = "1") @PathVariable Long id) {
        Boolean success =  reservationService.updateReservationById(id);
        return success ? success() : error("取消失败，可能已被取消或不存在");
    }
}
